
var map;
var directions;
var directionsService = new google.maps.DirectionsService();

var route;
var routeIndex=0;
var routeTraveled = 0;

var traveledDistance;

var markerPlayer;

// Initialize stuff
function initialize() {
	
	navigator.geolocation.getCurrentPosition(success,failure)
	
}

function failure(error)
{
	alert("Failure - "+error.message)
}

function success(position){
	
	var latitude = position.coords.latitude;
	var longitude = position.coords.longitude;	

	var mapDiv = document.getElementById('map-canvas');
	// By using the 'maximumAge' option above, the position
	// object is guaranteed to be at most 10 minutes old.
	
	var start = new google.maps.LatLng(latitude,longitude);
	var oceanBeach = new google.maps.LatLng(37.7683909618184, -122.51089453697205);
	
	map = new google.maps.Map(mapDiv, {
		center: start,
		zoom: 20,
		disableDefaultUI: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP});
	
	markerPlayer = new google.maps.Marker({
      position: start,
      map: map
    });
	
	directions= new google.maps.DirectionsRenderer();
	directions.setMap(map);
	
	calcRoute(start,oceanBeach)
	

	setInterval(run,33);
}

function calcRoute(start,end) {
	
	// The request
	var request = {
		origin:start, 
		destination:end,
		travelMode: google.maps.DirectionsTravelMode.WALKING
	};
	
	// Get the route
	directionsService.route(request, function(result, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			
			// Set the directions
			directions.setDirections(result);
			
			// SAve the route
			route = new Array();
			
			for(var i=0;i<result.routes[0].legs[0].steps.length;i++){
			
				var step = result.routes[0].legs[0].steps[i]				
				
				for(var j=0;j<step.path.length;j++){
					
					var path = step.path[j]
					
					route.push(path)
				}
			}
			
			markerPlayer.setPosition(route[0])
			
			// Add markers
			//addMarkers();
		}
	});
}


function addMarkers() {
		
		// For each step in the routes legs
	for(var k=0;k<route.length;k++){
		
		var step = route[k]
		
		// Place a marker
		var marker = new google.maps.Marker({
			position: step,
			map: map
		});
	}
}

var cKey=0;
var bikeVelocity = 0;

function handleInput()
{
	var rightKey = keyIsDown(37)
	var leftKey = keyIsDown(39)
	
	releaseKey(37)
	releaseKey(39)
	
	var milesPerHour = 35
	var kilometersPerSecond = milesPerHour * 0.0004
	var bikeMaxSpeed = kilometersPerSecond // Kilometers per second
	var distancePerUpdate = bikeMaxSpeed / 33
	
	if(cKey==0||(cKey==1&&rightKey)){
		routeTraveled+= (distancePerUpdate-bikeVelocity)/3;
		cKey=-1
	}
	else if(cKey==0||(cKey==-1&&leftKey)){
		bikeVelocity+= (distancePerUpdate-bikeVelocity)/3;
		cKey=1
	}else
	{
		bikeVelocity*=.999
	}
}

function run()
{
	if(route==null)return;
	if(route.length<=0)return;
	
	// Handle player input
	handleInput();
	
	//
	routeTraveled +=bikeVelocity;
	
	var c1 = route[routeIndex]
	var c2 = route[routeIndex+1]	
	
	var stepDistance = coordinateDistance(c1,c2)
	
	if(routeTraveled>=stepDistance){
		
		routeTraveled =0;
		routeIndex++;
	
		c1 = route[routeIndex]
		c2 = route[routeIndex+1]
		
		stepDistance = coordinateDistance(c1,c2)
	}
	
	var interval = routeTraveled / stepDistance
	
	// Change the players position
	markerPlayer.setPosition(calculateProgressLocation(c1,c2,interval));
	
	// Distance from the center
	var centerDistance = coordinateDistance(markerPlayer.getPosition(),map.getCenter())
	
	// Center the screen if we are far enough from it
	if(centerDistance >.05)map.panTo(markerPlayer.getPosition())
	
	// Set the zoom
	map.setZoom(20)
}

google.maps.event.addDomListener(window, 'load', initialize);